<?php echo $this->Html->script('kindeditor/kindeditor.js');?>
<?php echo $this->Form->create("Blog");?>
	<link href="js/popup_layer/styles/core.css" type="text/css" rel="stylesheet" />
	<div id = "write">
		<div id="wmain">
			<span id = "wtitle">
				<label>标题:</label><?php echo $this->Form->input("title",array('label'=>false,'div'=>'titleoutline','value'=>$blog['Blog']['title']));?>
			</span>

			<div class="postArea">
				<?php echo $this->Form->input("content",array('label'=>false,'id'=>'textitem','value'=>$blog['Blog']['content']));?>
				<div id = "tags" class='postAreaDiv'>
					<label class="post_label">标签:</label>
					<?php echo $this->Form->input("keywords",array('label'=>false,'id'=>'tagName','value'=>$blog['Blog']['keywords']));?>
					<input id='autoTags' type='button' value='自动获取标签'/>
				</div>
				<div id="category" class='postAreaDiv'>
					<label class='post_labek'>分类:</label>
					<select id='categories' name='data[Blog][cid]'>
						<?php
							if(is_array($BlogsCategorys)){
								foreach($BlogsCategorys as $category){
									echo "<option value='{$category['BlogsCategory']['id']}' ";
										if($category['BlogsCategory']['id'] == $blog['Blog']['cid']){
											echo "selected='selected'";
										}
									echo ">{$category['BlogsCategory']['name']}</option>";
								}
							}
						?>
						<option id ="addCategory" value=''>点击添加分类..</option>
					</select>
				</div>
				<div id='readable' class='postAreaDiv'>
					<label class='post_label'>查看权限:</label>
					<input type='radio' name='data[Blog][visible]' value='1' <?php if($blog['Blog']['visible'] ==='1'){echo "checked='checked'"; } ?>/>公开
					<input type='radio' name='data[Blog][visible]' value='0' <?php if($blog['Blog']['visible'] ==='0'){echo "checked='checked'"; } ?>/>私有
					<div id="textEnd">
						<input type='hidden' name='data[Blog][id]' value='<?php echo $blog['Blog']['id']?>' />
						<input type="submit" value="提交" id="submit_btn" />
						<input type="button" value="存为草稿" id="draft_btn" />
						<input type="button" value="取消" id="cancel_btn" />
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
<script>
//标题的outline
	$('#BlogTitle').focus(function(){
		$('.titleoutline').css({'border':'none',
								'border-radius':'15px',
								'-moz-border-radius':'15px',
								'-webkit-border-radius':'23px',
								'box-shadow':'0 0 3px 2px #15a',
								'-moz-box-shadow':'0 0 3px 2px #15a',
								'-webkit-box-shadow':'0 0 2px 3px #37c',
								'margin':'2px 0 1px 2px'});
		$('#BlogTitle').css({'border':'none',
								'margin':'-1px 0 0 -1px',
								'box-shadow':'0px 1px 2px 0 #999 inset,0px 6px 8px 0 #ccc inset',
								'-moz-box-shadow':'0px 1px 2px 0 #999 inset,0px 6px 8px 0 #ccc inset'});
	})
	$('#BlogTitle').blur(function(){
		$('.titleoutline').css({'border-radius':'none',
								'-moz-border-radius':'none',
								'-webkit-border-radius':'none',
								'box-shadow':'none',
								'-moz-box-shadow':'none',
								'-webkit-box-shadow':'none',
								'margin':'0'});
		$('#BlogTitle').css({'border':'1px solid #bbb',
								'margin':'0',
								'box-shadow':'0px 1px 2px 0 #AAAAAA inset,0px 6px 8px 0 #DDDDDD inset',
								'-moz-box-shadow':'0px 1px 2px 0 #AAAAAA inset,0px 6px 8px 0 #DDDDDD inset'});
	})
// //添加自定义标签
 	// $("#tagName") .focus(function(){
		// $("#tagTips").val("");
		// $("#tagTips").css("color","#fff");
	// })
	// $("#tagName").blur(function(){
		// $("#tagTips").val("多个标签用空格隔开");
		// $("#tagTips").css("color","#999");
	// })
//加载编辑器
	// KE.show({
		// id : 'textitem',
		// width : '100%',
		// resizeMode : 0
	// });
//添加目录
	// new PopupLayer({trigger:"#addCategory",popupBlk:"#blk1",closeBtn:"#close",offsets:{
			// x:100,
			// y:-300
		// }});
	$("#addCategory").click(function(){
		$("#blk1").css("display","block");
		$('#categories').get(0).selectedIndex = 0;
		$('#new_folder_name')[0].focus();
	});
	$('#close').click(function(){
		$("#blk1").css("display","none");
	});
//工具条消失
	$("#fixedItem").css("display","none");
	$("#wheader").html("&nbsp;&nbsp;写日志");

//自动获取标签
	$("#autoTags").click(function(){
		KE.sync('textitem');
		if($("#BlogTitle").val() == ''){
			alert("文章标题不能为空");
			return false;
		}
		if($("#textitem").val() == ''){
			alert("文章内容不能为空");
			return false;
		}
		$.post("keywords",{title:$("#BlogTitle").val(),content:KE.text('textitem').replace(/\s+/,' ')},function(data){
			if(data != '0'){
				$("#tagName").val(data);
			};
		})
	});
</script>
        <div id="blk1" class="blk" style="display:none;">
            <div class="head"><div class="head-right"></div></div>
            <div class="main">
                <h2>添加自定义分类</h2>
                <a href="javascript:void(0)" id="close" class="closeBtn">关闭</a>
				<form id="add_blog_folder_form" action="Categories/add_blog_folder" method='POST'>
					<input id="new_folder_name" type="text" maxlength="20" name="name" />
					<input id="surebtn" type="button" value="添加"/>
				</form>
            </div>
            <div class="foot"><div class="foot-right"></div></div>
        </div>
<script>
// $("#add_blog_folder_form").ajaxForm();
$("#surebtn").click(function(){
	if($("#new_folder_name").val() == ''){
		alert("不能添加空目录");
		return false;
	}
	$("#add_blog_folder_form").ajaxSubmit(function(data){
		if(data == '' || data == '0'){
			alert ("添加目录失败,请重试.");
		}else{
			$('#categories').prepend("<option value='"+data+"'>"+$("#new_folder_name").val()+"</option>");
			$("#blk1").css("display","none");
			$('#categories').get(0).selectedIndex = 0;
		}
	});
	// return false;
});
$("#submit_btn").click(function(){
	KE.sync('textitem');
	if($("#BlogTitle").val() == ''){
		alert("文章标题不能为空");
		return false;
	}
	if($("#textitem").val() == ''){
		alert("文章内容不能为空");
		return false;
	}
	$("#BlogEditForm").attr('action','blogs/edit')
	$("#BlogEditForm").ajaxSubmit(function(data){
		if(data == '1'){
			alert ("发表成功");
		}else{
			alert ("发表失败");
		}
	});
	return false;
});
var draft_id = null;
var is_numeric_reg = new RegExp("^[0-9]+$");
$("#draft_btn").click(function(){
	KE.sync('textitem');
	if($("#BlogTitle").val() == ''){
		alert("文章标题不能为空");
		return false;
	}
	if($("#textitem").val() == ''){
		alert("文章内容不能为空");
		return false;
	}
	if(draft_id != null){
		$("#BlogEditForm").attr('action','blogs/keep_draft/'+draft_id);
	}else{
		$("#BlogEditForm").attr('action','blogs/keep_draft');
	}
	$("#BlogEditForm").ajaxSubmit(function(data){
		if((data != '0') && (is_numeric_reg.test(data))){
			alert ("草稿保存成功");
			draft_id = data;
		}else{
			alert ("草稿保存失败");
		}
	});
	return false;
});
</script>
